﻿using System.Collections.Generic;
using LZX.MEditor.Enum;
using LZX.MEditor.MScriptableObject;
using UnityEngine.UIElements;

namespace LZX.MEditor.Window.Item
{
    public class RunTimeBundle:RunTimeItemBase
    {
        public Bundle Bundle;
        public RunTimeBundle(Bundle bundle,VisualElement scv)
        {
            Bundle = bundle;
            scv.Add(root);
            Name.text = bundle.Name;
            Count.text = string.Join(",", bundle.Group);
            SizeContainer.style.display = DisplayStyle.None;
            BG.style.display = DisplayStyle.None;
            InitAsset();
        }
        public Dictionary<string, RunTimeAsset> Assets = new Dictionary<string, RunTimeAsset>();
        private void InitAsset()
        {
            foreach (var guid in Bundle.AssetGUIDs)
            {
                Asset asset = new Asset(guid);
                RunTimeAsset assetitem = new RunTimeAsset(asset, DependenciesContainer);
                Assets.Add(asset.LoadPath, assetitem);
            }
        }
        public void OnLoadAsset(string assetName)
        {
            if (Assets.TryGetValue(assetName, out RunTimeAsset assetitem))
            {
                assetitem.SetLoadBG(true);
                assetitem.Count.text = LoadStatuHelper.LoadedAssets[assetName].count.ToString();
                assetitem.Size.text = LoadStatuHelper.LoadedAssets[assetName].size.ToString();
            }
        }
        public void OnUnLoadAsset(string assetName)
        {
            if (Assets.TryGetValue(assetName, out RunTimeAsset assetitem))
            {
                var count = LoadStatuHelper.LoadedAssets[assetName].count;
                if(count == 0)
                    assetitem.SetLoadBG(false);
                assetitem.Count.text = count.ToString();
                assetitem.Size.text = LoadStatuHelper.LoadedAssets[assetName].size.ToString();
            }
        }
        public void OnUnLoadAllAsset(bool unloadUnloadedAssets)
        {
            if(!unloadUnloadedAssets)
                return;
            foreach (var asset in Assets.Values)
            {
                asset.SetLoadBG(false);
                asset.Count.text = "0";
                asset.Size.text = "0";
            }
        }
    }
}